Supported with v2.0.6+
Determined Dice allows you to show the dice rolling with physics, but have their value determined beforehand. This can be used if you are showing results that came from elsewhere, like online play or bluetooth dice, but still want to show the physical roll happening. This is setup in a way that you can create your own determined dice, but we have a few prefabs available to easily start using the feature.
We have a D4, D6, D8, and D20 available as prefabs currently. You can add any of those to your project through Gameboard > Tools > Determined Dice > Add Determined {DiceSize}
When using a die that is properly setup with the GameboardPredeterminedDice
script, you can manipulate that die so the roll is a specific face after the physics roll. This script is specific to rolling an individual die, if you want to roll multiple dice that interact you will want to use GameboardDiceManager
in conjunction with the GameboardPredeterminedDice
script so the simulated physics match up properly.
First, you need a reference to the predetermined die script. You can get it programatically or set it through the Unity editor.
public GameboardPredeterminedDice predeterminedDie;
Before rolling the die, you will want to setup however you plan on throwing the dice with physics. You can set this up by subscribing to the ThrowDice
event. Whatever method you setup here will run before rolling the determined die to do the physics desired.
You can also subscribe to the BeforeDiceThrow
event. This will run before simulating physics, so if you want to reset the dice location or anything like that, this would be a good place to do it.
void Start()
{
predeterminedDie.BeforeDiceThrow += Reset;
predeterminedDie.ThrowDice += ThrowDice;
}
void Reset()
{
// Do whatever you want to the dice before the simulated physics start recording
}
void ThrowDice()
{
//Apply the physics you want to to throw the dice
//e.g.(assuming this script is also attached to the dice rigibody)
Rigidbody rb = GetComponent<Rigidbody>();
var xTorque = Random.Range(0, 100);
var yTorque = Random.Range(0, 100);
var zTorque = Random.Range(0, 100);
rb.useGravity = true;
rb.AddTorque(xTorque, yTorque, zTorque);
}
To roll an individual die with a determined value, you want to call the method RollDeterminedValue
passing an int representing the side you want to appear after the roll.
public void RollDeterminedDice(int diceValue)
{
predeterminedDie.RollDeterminedValue(diceValue);
}
Now, the dice will simulate the physics and modify the mesh so the dice value you provided will be the side appearing up after the roll.
Using the dice manager to roll multiple predetermined dice is very similar to rolling an individual die, it just allows multiple die to be specified so their physics are simulated together before landing on their determined face.
First, you need a reference to the dice manager script. You can get it programatically or set it through the Unity editor.
public GameboardDiceManager diceManager;
You want all the dice that use the GameboardPredeterminedDice
script that you plan to throw together in the Dice
list. You can add them in the Unity editor or programatically.
Before rolling the die, you will want to setup however you plan on throwing the dice with physics. You can set this up by subscribing to the ThrowMultipleDice
event. Whatever method you setup here will run before rolling the determined die to do the physics desired.
You can also subscribe to the BeforeDiceThrow
event. This will run before simulating physics, so if you want to reset the dice location or anything like that, this would be a good place to do it.
void Start()
{
diceManager.BeforeDiceThrow += Reset;
diceManager.ThrowMultipleDice += ThrowDice;
}
void Reset()
{
// Do whatever you want to the dice before the simulated physics start recording
}
void ThrowDice()
{
//Apply the physics you want to to throw the dice
//e.g.(assuming this script is also attached to the dice rigibody)
Rigidbody rb = GetComponent<Rigidbody>();
var xTorque = Random.Range(0, 100);
var yTorque = Random.Range(0, 100);
var zTorque = Random.Range(0, 100);
rb.useGravity = true;
rb.AddTorque(xTorque, yTorque, zTorque);
}
To roll the dice with a determined value, you can call the method RollMultipleDeterminedValueDice
with an array of ints indicating which sides you want to appear if the dice all have the same number of sides. If you have mixed dice sizes or you want to make sure specific dice have specific values, you can call RollMultipleDeterminedValueDice
with a list of MultiDiceRoll
objects.
public void RollDeterminedDice()
{
//The dice all have the same number of sides
diceManager.RollMultipleDeterminedValueDice(new int[] { 1, 2, 3, 4, 5, 6 });
//You want specific die to have specific values / they are different sizes
diceManager.RollMultipleDeterminedValueDice(diceManager.dice.Select(die => new MultiDiceRoll()
{
face = int.Parse(die.id.Split('_')[1]),
die = die
}
).ToList());
}
Now, the dice will simulate the physics all together and modify the mesh so the dice values provided will be appearing up after the roll.
You can use our prefabs, but you can also create your own dice setups. You need the following when setting up your own dice:
GameboardPredeterminedDice
script, a Collider, and a RigibodyGameboardDiceSide
script, specifying location and valueThe predetermined dice works by keeping the collider/rigibody the same while rotating the child mesh renderer to the specified rotation in the rotation map list, so the structure of the die needs to be as follows:
You can get a rough template of the dice by selecting an empty GameObject and going to Gameboard > Tools > Add Predetermined Dice Template
This will generate a rough template to create your own dice.
The GameObject representing the Die with the Gameboard Predetermined Dice
script will include the following:
Gameboard Dice Side
that are children of the Mesh Renderer, representing each side of the die. Make sure to remove any old sides, since sides will be added automatically.